package com.example.homework_03;

import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

import java.util.HashSet;
import java.util.Iterator;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btnNew;
    private Button btnOld;
    private Button btnChoice ;
    private Button btnSingleChoice;
    private Button btnMixChoice;
    private Button btnCustom ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        btnOld = (Button) findViewById(R.id.button1);  //旧的
        btnNew = (Button) findViewById(R.id.button2);  //新的
        btnChoice = (Button) findViewById(R.id.button3); //选择对话窗口
        btnSingleChoice = (Button) findViewById(R.id.button4); //单选择对话窗口
        btnMixChoice = (Button) findViewById(R.id.button5); //多选择对话窗口
        btnCustom = (Button) findViewById(R.id.button) ; //自定义对话框

        btnOld.setText("老版对话框");
        btnNew.setText("新版对话框");
        btnChoice.setText("选择对话框");
        btnSingleChoice.setText("单选对话框");
        btnMixChoice.setText("复选对话框");
        btnCustom.setText("自定义对话框");

        btnNew.setOnClickListener(this);
        btnOld.setOnClickListener(this);
        btnChoice.setOnClickListener(this);
        btnSingleChoice.setOnClickListener(this);
        btnMixChoice.setOnClickListener(this);
        btnCustom.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button1:
                //创建旧版对话框
                AlertDialog.Builder oldalter = new OldAlter().createOldAlter(this);
                oldalter.show();
                break;

            case R.id.button2:
                //新版对话框
                NewAlter newalter = new NewAlter();
                newalter.show(this.getSupportFragmentManager(), "新的");
                break;

            case R.id.button3:
                //选择对话框
                ChoiceDialog choiceDialog = new ChoiceDialog(new ChoiceDialog.ChoiceListener() {
                    @Override
                    public void onClicked(String choice) {
                        //实现选择结果后的方法
                        Toast toast = Toast.makeText(getApplicationContext(),"你选择的是"+choice,Toast.LENGTH_SHORT);
                        toast.show();
                    }
                }) ;
                choiceDialog.show(this.getSupportFragmentManager(),"选择");
                break;

            case R.id.button4:
                //单选对话框
                SingleChoiceDialog singleChoiceDialog = new SingleChoiceDialog(this, new SingleChoiceDialog.SingleChoiceListener() {
                    @Override
                    public void onClicked(String choice) {
                        Toast toast = Toast.makeText(getApplicationContext(),"你选择了"+choice,Toast.LENGTH_SHORT) ;
                        toast.show();
                    }
                });
                singleChoiceDialog.show(this.getSupportFragmentManager(),"单选");
                break;

            case R.id.button5:
                //复选框
                MixChoiceDialog mixChoiceDialog = new MixChoiceDialog(this, new MixChoiceDialog.MixChoiceListener() {
                    @Override
                    public void onClicked(HashSet colorset) {
                        Iterator iterator = colorset.iterator() ;
                        String str ="你选择了：";
                        while (iterator.hasNext()){
                            String temp = (String) iterator.next() ;
                            str += temp+"," ;
                        }
                        Toast toast = Toast.makeText(getApplicationContext(),str,Toast.LENGTH_SHORT) ;
                        toast.show();
                    }
                });
                mixChoiceDialog.show(this.getSupportFragmentManager(),"复选");
                break;

            case R.id.button:
                CustomDialog customDialog = new CustomDialog(new CustomDialog.CustomListener() {
                    @Override
                    public void onclick(String str1, String str2) {
                        Toast toast = Toast.makeText(getApplicationContext(),str1+"\n"+str2,Toast.LENGTH_SHORT);
                        toast.show();
                    }
                }) ;
                customDialog.show(this.getSupportFragmentManager(),"自定义");
                break ;
        }
    }




}

class OldAlter {
    public AlertDialog.Builder createOldAlter(final Context context) {
        return new AlertDialog.Builder(context).setTitle("Dialog对话框！")
                .setMessage("想退出码？")
                .setIcon(R.drawable.ic_launcher_background)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast toast = Toast.makeText(context.getApplicationContext(), "退出成功", Toast.LENGTH_SHORT);
                        toast.show();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast toast = Toast.makeText(context.getApplicationContext(), "取消", Toast.LENGTH_SHORT);
                        toast.show();
                    }
                });
    }
}





/*

    class NegativeMsg implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            Toast toast = Toast.makeText(getApplicationContext(),"旧的",Toast.LENGTH_SHORT) ;
            toast.show();
        }
    }

    class PositiveMsg implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            Toast toast = Toast.makeText(getApplicationContext(),"旧的",Toast.LENGTH_SHORT) ;
            toast.show();
        }
    }
 */

